/*
 *
 *  Copyright (C) 2012-2024
 *  All rights reserved, Designed By 杭州连线客信息技术有限公司
 *  注意：
 *  本软件为杭州连线客信息技术有限公司开发研制，未经购买不得使用
 *  购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
 *  一经发现盗用、分享等行为，将追究法律责任，后果自负
 *
 */

package com.lxk.oms.flow.comment;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.lxk.oms.order.domain.bo.OmsOrderCommentItemBo;
import com.lxk.oms.order.domain.req.CommentOrderReq;
import com.lxk.oms.order.domain.vo.OmsOrderItemVo;
import com.lxk.oms.order.domain.vo.OmsOrderVo;
import com.lxk.oms.order.service.IOmsOrderItemService;
import com.lxk.oms.order.service.IOmsOrderService;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.FlowExecutor;
import com.yomahub.liteflow.core.NodeComponent;
import com.yomahub.liteflow.log.LFLog;
import com.yomahub.liteflow.log.LFLoggerManager;
import lombok.AllArgsConstructor;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StreamUtils;
import org.dromara.common.core.utils.ValidationUtils;

import java.util.List;
import java.util.Map;

import static com.lxk.oms.flow.comment.FlowCommentOrderNodeConst.COMMENT_FILL_ORDER_NODE;

/**
 * 填充对应的上下文
 *
 * @author heyi
 * 2024/9/3
 */
@AllArgsConstructor
@LiteflowComponent(COMMENT_FILL_ORDER_NODE)
public class CommentFillNode extends NodeComponent {
    private final IOmsOrderService omsOrderService;
    private final IOmsOrderItemService omsOrderItemService;
    private final LFLog log = LFLoggerManager.getLogger(FlowExecutor.class);

    @Override
    public void beforeProcess() {
        CommentOrderReq request = this.getRequestData();
        ValidationUtils.validate(request);
        log.info("confirmOrderNode beforeProcess,{}", request);
    }

    @Override
    public void process() throws Exception {
        CommentOrderReq request = this.getRequestData();
        CommentOrderContext contextBean = this.getContextBean(CommentOrderContext.class);
        Long userId = request.getUserId();
        String userType = request.getUserType();
        String orderNo = request.getOrderNo();
        OmsOrderVo omsOrderVo = omsOrderService.queryByOrderSn(orderNo, userId);
        if (ObjectUtil.isNull(omsOrderVo)) {
            log.error("评论 订单不存在:{},{}", orderNo, userId);
            throw new ServiceException("订单不存在");
        }
        contextBean.setOmsOrderVo(omsOrderVo);
        contextBean.setUserId(userId);
        contextBean.setOrderNo(orderNo);
        contextBean.setUserType(userType);
        contextBean.setComment(request.getComment());
        contextBean.setCommentPic(request.getCommentPic());
        contextBean.setCommentVideo(request.getCommentVideo());
        contextBean.setPositiveScore(request.getPositiveScore());
        contextBean.setSendOptionEvent(request.getSendOptionEvent());

        // 子订单评论
        List<CommentOrderReq.CommentItemReq> itemList = request.getItemList();
        if (CollUtil.isNotEmpty(itemList)) {
            itemList.forEach(ValidationUtils::validate);
            List<Long> orderItemIdList = StreamUtils.toList(itemList, CommentOrderReq.CommentItemReq::getOrderItemId);
            List<OmsOrderItemVo> omsOrderItemVos = omsOrderItemService.queryByIdList(orderItemIdList);
            Map<Long, OmsOrderItemVo> map = omsOrderItemService.convertToMap(omsOrderItemVos);
            contextBean.setOrderItemVoList(omsOrderItemVos);
            List<OmsOrderCommentItemBo> list = itemList.stream().map(item -> {
                OmsOrderCommentItemBo add = MapstructUtils.convert(item, OmsOrderCommentItemBo.class);
                OmsOrderItemVo omsOrderItemVo = map.get(add.getOrderItemId());
                if (ObjectUtil.isNotNull(omsOrderItemVo)) {
                    add.setSpuId(omsOrderItemVo.getSpuId());
                    add.setSkuId(omsOrderItemVo.getSkuId());
                    add.setOrderId(omsOrderVo.getId());
                    add.setMerId(omsOrderVo.getMerId());
                    add.setUserId(omsOrderVo.getUserId());
                    add.setOrderType(omsOrderVo.getOrderType());
                }
                return add;
            }).toList();
            contextBean.setItemList(list);
        }
        ValidationUtils.validate(contextBean);
    }
}
